package com.eshop.modules.mp.config;

import com.google.common.collect.Maps;
import me.chanjar.weixin.mp.api.WxMpMessageHandler;
import me.chanjar.weixin.mp.config.WxMpConfigStorage;
import me.chanjar.weixin.mp.api.impl.WxMpServiceImpl;
import com.eshop.utils.RedisUtil;
import me.chanjar.weixin.mp.config.impl.WxMpDefaultConfigImpl;
import com.eshop.utils.ShopKeyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import com.eshop.utils.RedisUtils;
import com.eshop.modules.mp.handler.ScanHandler;
import com.eshop.modules.mp.handler.SubscribeHandler;
import com.eshop.modules.mp.handler.UnsubscribeHandler;
import com.eshop.modules.mp.handler.MsgHandler;
import com.eshop.modules.mp.handler.MenuHandler;
import com.eshop.modules.mp.handler.LocationHandler;
import com.eshop.modules.mp.handler.StoreCheckNotifyHandler;
import com.eshop.modules.mp.handler.KfSessionHandler;
import com.eshop.modules.mp.handler.NullHandler;
import com.eshop.modules.mp.handler.LogHandler;
import me.chanjar.weixin.mp.api.WxMpMessageRouter;
import me.chanjar.weixin.mp.api.WxMpService;
import java.util.Map;
import org.springframework.context.annotation.Configuration;

@Configuration(proxyBeanMethods = false)
public class WxMpConfiguration
{
    private static Map<String, WxMpService> mpServices;
    private static Map<String, WxMpMessageRouter> routers;
    private static LogHandler logHandler;
    private static NullHandler nullHandler;
    private static KfSessionHandler kfSessionHandler;
    private static StoreCheckNotifyHandler storeCheckNotifyHandler;
    private static LocationHandler locationHandler;
    private static MenuHandler menuHandler;
    private static MsgHandler msgHandler;
    private static UnsubscribeHandler unsubscribeHandler;
    private static SubscribeHandler subscribeHandler;
    private static ScanHandler scanHandler;
    private static RedisUtils redisUtils;
    
    @Autowired
    public WxMpConfiguration(final LogHandler logHandler, final NullHandler nullHandler, final KfSessionHandler kfSessionHandler, final StoreCheckNotifyHandler storeCheckNotifyHandler, final LocationHandler locationHandler, final MenuHandler menuHandler, final MsgHandler msgHandler, final UnsubscribeHandler unsubscribeHandler, final SubscribeHandler subscribeHandler, final ScanHandler scanHandler, final RedisUtils redisUtils) {
        WxMpConfiguration.logHandler = logHandler;
        WxMpConfiguration.nullHandler = nullHandler;
        WxMpConfiguration.kfSessionHandler = kfSessionHandler;
        WxMpConfiguration.storeCheckNotifyHandler = storeCheckNotifyHandler;
        WxMpConfiguration.locationHandler = locationHandler;
        WxMpConfiguration.menuHandler = menuHandler;
        WxMpConfiguration.msgHandler = msgHandler;
        WxMpConfiguration.unsubscribeHandler = unsubscribeHandler;
        WxMpConfiguration.subscribeHandler = subscribeHandler;
        WxMpConfiguration.scanHandler = scanHandler;
        WxMpConfiguration.redisUtils = redisUtils;
    }
    
    public static WxMpService getWxMpService() {
        WxMpService wxMpService = WxMpConfiguration.mpServices.get(ShopKeyUtils.getYshopWeiXinMpSevice());
        if (wxMpService == null || WxMpConfiguration.redisUtils.get(ShopKeyUtils.getYshopWeiXinMpSevice()) == null) {
            final WxMpDefaultConfigImpl configStorage = new WxMpDefaultConfigImpl();
            configStorage.setAppId((String)RedisUtil.get(ShopKeyUtils.getWechatAppId()));
            configStorage.setSecret((String)RedisUtil.get(ShopKeyUtils.getWechatAppSecret()));
            configStorage.setToken((String)RedisUtil.get(ShopKeyUtils.getWechatToken()));
            configStorage.setAesKey((String)RedisUtil.get(ShopKeyUtils.getWechatEncodingAESKey()));
            wxMpService = (WxMpService)new WxMpServiceImpl();
            wxMpService.setWxMpConfigStorage((WxMpConfigStorage)configStorage);
            WxMpConfiguration.mpServices.put(ShopKeyUtils.getYshopWeiXinMpSevice(), wxMpService);
            WxMpConfiguration.routers.put(ShopKeyUtils.getYshopWeiXinMpSevice(), newRouter(wxMpService));
            WxMpConfiguration.redisUtils.set(ShopKeyUtils.getYshopWeiXinMpSevice(), (Object)"yshop");
        }
        return wxMpService;
    }
    
    public static void removeWxMpService() {
        WxMpConfiguration.redisUtils.del(new String[] { ShopKeyUtils.getYshopWeiXinMpSevice() });
        WxMpConfiguration.mpServices.remove(ShopKeyUtils.getYshopWeiXinMpSevice());
        WxMpConfiguration.routers.remove(ShopKeyUtils.getYshopWeiXinMpSevice());
    }
    
    public static WxMpMessageRouter getWxMpMessageRouter() {
        final WxMpMessageRouter wxMpMessageRouter = WxMpConfiguration.routers.get(ShopKeyUtils.getYshopWeiXinMpSevice());
        return wxMpMessageRouter;
    }
    
    private static WxMpMessageRouter newRouter(final WxMpService wxMpService) {
        final WxMpMessageRouter newRouter = new WxMpMessageRouter(wxMpService);
        newRouter.rule().handler((WxMpMessageHandler)WxMpConfiguration.logHandler).next();
        newRouter.rule().async(false).msgType("event").event("kf_create_session").handler((WxMpMessageHandler)WxMpConfiguration.kfSessionHandler).end();
        newRouter.rule().async(false).msgType("event").event("kf_close_session").handler((WxMpMessageHandler)WxMpConfiguration.kfSessionHandler).end();
        newRouter.rule().async(false).msgType("event").event("kf_switch_session").handler((WxMpMessageHandler)WxMpConfiguration.kfSessionHandler).end();
        newRouter.rule().async(false).msgType("event").event("poi_check_notify").handler((WxMpMessageHandler)WxMpConfiguration.storeCheckNotifyHandler).end();
        newRouter.rule().async(false).msgType("event").event("click").handler((WxMpMessageHandler)WxMpConfiguration.menuHandler).end();
        newRouter.rule().async(false).msgType("event").event("view").handler((WxMpMessageHandler)WxMpConfiguration.menuHandler).end();
        newRouter.rule().async(false).msgType("event").event("scancode_waitmsg").handler((WxMpMessageHandler)WxMpConfiguration.menuHandler).end();
        newRouter.rule().async(false).msgType("event").event("subscribe").handler((WxMpMessageHandler)WxMpConfiguration.subscribeHandler).end();
        newRouter.rule().async(false).msgType("event").event("unsubscribe").handler((WxMpMessageHandler)WxMpConfiguration.unsubscribeHandler).end();
        newRouter.rule().async(false).msgType("event").event("LOCATION").handler((WxMpMessageHandler)WxMpConfiguration.locationHandler).end();
        newRouter.rule().async(false).handler((WxMpMessageHandler)WxMpConfiguration.msgHandler).end();
        return newRouter;
    }
    
    static {
        WxMpConfiguration.mpServices = Maps.newHashMap();
        WxMpConfiguration.routers = Maps.newHashMap();
    }
}
